﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ProjectShop
{
    public partial class FrmShopInfo : Form
    {
        ProjectCommon.ClassCommon cc;
        private ProjectCommon.ClassCommon.OperateState ccosState;
        private ClassShop csShop;

        public FrmShopInfo(ProjectCommon.ClassCommon aClassCommon, ProjectCommon.ClassCommon.OperateState ccos, string FastCode)
        {
            InitializeComponent();
            cc = aClassCommon;
            ccosState = ccos;
            switch (ccosState)
            {
                case ProjectCommon.ClassCommon.OperateState.osNew: ShopNewInitial(); break;
                case ProjectCommon.ClassCommon.OperateState.osModify: ShopModifyInitial(FastCode); break;
                case ProjectCommon.ClassCommon.OperateState.osView: ShopViewInitial(FastCode); break;
            }
            ProjectCommon.ClassCommon.CopyComboBoxItem(csShop.cbShopState, cbShopState);
            ProjectCommon.ClassCommon.CopyComboBoxItem(csShop.cbShopLever, cbShopLever);
            ProjectCommon.ClassCommon.CopyComboBoxItem(csShop.cbCircuitTypes, cbCircuitTypes);
            ProjectCommon.ClassCommon.CopyComboBoxItem(csShop.cbOwningCompany, cbOwningCompany);
        }

        private bool ShopNewInitial()
        {
            bool result = false;
            this.Text += "---添加";
            csShop = new ClassShop(cc.scKengee_IT_MIS);
            csShop.ShopNewInitial();
            lblPartnerNumber.Visible = true;
            tbPartnerNumber.Visible = true;
            lblTerminalNumber.Visible = true;
            tbTerminalNumber.Visible = true;
            return result;
        }

        private bool ShopModifyInitial(string FastCode)
        {
            bool result = false;
            this.Text += "---修改";
            csShop = new ClassShop(cc.scKengee_IT_MIS);
            result = csShop.GetShop_FastCode(FastCode);
            ProjectCommon.ClassCommon.SetClass2Form(csShop.dtShop, this.Controls);
            lblPartnerNumber.Visible = true;
            tbPartnerNumber.Visible = true;
            lblTerminalNumber.Visible = true;
            tbTerminalNumber.Visible = true;
            return result;
        }

        private bool ShopViewInitial(string FastCode)
        {
            bool result = false;
            this.Text += "---浏览";
            csShop = new ClassShop(cc.scKengee_IT_MIS);
            result = csShop.GetShop_FastCode(FastCode);
            ProjectCommon.ClassCommon.SetClass2Form(csShop.dtShop, this.Controls);
            string sPowerRight = cc.dtLoginUser.Rows[0]["PowerRight"].ToString();
            if (sPowerRight[1] == '1')
            {
                lblPartnerNumber.Visible = true;
                tbPartnerNumber.Visible = true;
                lblTerminalNumber.Visible = true;
                tbTerminalNumber.Visible = true;
            }
            btnSave.Visible = false;
            return result;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private bool FastCodeOtherExist(string aFastCode)
        {
            bool result = false;
            ClassShop csTemp = new ClassShop(cc.scKengee_IT_MIS);

            if (csTemp.GetShop_FastCode(aFastCode))
            {
                string TempID = csTemp.dtShop.Rows[0]["ID"].ToString();
                string ThisID = csShop.dtShop.Rows[0]["ID"].ToString();
                if (TempID.Equals(ThisID))
                {
                    result = false;
                }
                else
                {
                    result = true;
                }
            }
            else
            {
                result = false;
            }

                
            return result;
        }


        private void btnOK_Click(object sender, EventArgs e)
        {
            if (tbShopNo.Text.Trim().Length < 1)
            {
                tbShopNo.Focus();
                MessageBox.Show("“门店编号”不可为空");
            }
            else if (tbFastCode.Text.Trim().Length < 1)
            {
                tbFastCode.Focus();
                MessageBox.Show("“快速编码”不可为空");

            }
            else if (FastCodeOtherExist(tbFastCode.Text.Trim()))
            {
                tbFastCode.Focus();
                MessageBox.Show("“快速编码”=" + tbFastCode.Text.Trim() + "已被使用，请更换！");
            }
            else
            {
                this.DialogResult = DialogResult.OK;
                switch (ccosState)
                {
                    case ProjectCommon.ClassCommon.OperateState.osNew: ShopInfoAppend(); break;
                    case ProjectCommon.ClassCommon.OperateState.osModify: ShopInfoModify(); break;
                    case ProjectCommon.ClassCommon.OperateState.osView: { }; break;
                }
                this.Close();
            }
        }

        private void ShopInfoAppend()
        {
            ProjectCommon.ClassCommon.SetForm2Class(csShop.dtShop, this.Controls);
            csShop.ShopAppend();
        }

        private void ShopInfoModify()
        {
            ProjectCommon.ClassCommon.SetForm2Class(csShop.dtShop, this.Controls);
            csShop.ShopModify();
        }
    }
}
